﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Web.Model;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Utility;
using Portal.Workflow.Bussiness;
using System.Data;
using Portal.Xml.Utility;
using Portal.Admin.Home.Render.Template;

namespace Portal.Systems.Render
{
    public class RoleRelationTemplateRenderBll
    {
        [AjaxPro.AjaxMethod]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                RoleRelationTemplateCls[] RoleRelationTemplates = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleRelationTemplate(SiteId, null);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string RetHtml =
                    " <script language=javascript>\r\n" +
                    "   function ClientAddRoleRelationTemplate()\r\n" +
                    "   {\r\n" +
                    "       window.open('AddRoleRelationTemplate.aspx','_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientUpdate(RoleRelationTemplateId)\r\n" +
                    "   {\r\n" +
                    "       window.open('UpdateRoleRelationTemplate.aspx?RoleRelationTemplateId='+RoleRelationTemplateId,'_self');\r\n" +
                    "   }\r\n" +

                    "   function ClientDelete(RoleRelationTemplateId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                    "       AjaxOut = Portal.Systems.Render.RoleRelationTemplateRenderBll.ServerSideDeleteRoleRelationTemplate(SiteId,RoleRelationTemplateId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divListForm').innerHTML=AjaxOut.HtmlContent;\r\n"+
                    "   }\r\n" +
                    " </script>\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientAddRoleRelationTemplate();\"><img class=image src=\"" + ButtonIconRoot + "/Add.png\"><div>Tạo mới</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    " <div id=divListForm>" + ServerSideReading(SiteId).HtmlContent + "</div>\r\n";

                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReading(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                RoleRelationTemplateCls[] RoleRelationTemplates = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleRelationTemplate(SiteId, null);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string RetHtml = "";
                   
                if (RoleRelationTemplates.Length == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">Chưa có dữ liệu nào trong chuyên mục này</div>";
                }
                else
                {
                    RetHtml +=
                        "<div class=\"tabcontent\">\r\n" +
                        "<table>" +
                        "<tr>\r\n" +
                        "    <th class=thIndex>#</th>\r\n" +
                        "    <th>Quan hệ vị trí</th>\r\n" +
                        "    <th></th>\r\n" +
                        "    <th></th>\r\n" +
                        "</tr>\r\n";

                    int StartIndex = 0;
                    int EndIndex = RoleRelationTemplates.Length;
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string UpdateButton = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + RoleRelationTemplates[iIndex].RoleRelationTemplateId + "');\">";
                        string DeleteButton = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + RoleRelationTemplates[iIndex].RoleRelationTemplateId + "');\">";
                        RetHtml +=
                           "    <tr>\r\n" +
                           "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                           "        <td>" + RoleRelationTemplates[iIndex].RoleRelationTemplateName + "</td>\r\n" +
                           "        <td style=\"width:10px;\">" + UpdateButton + "</td>\r\n" +
                           "        <td style=\"width:10px;\">" + DeleteButton + "</td>\r\n" +
                           "   </tr>\r\n";

                    }
                    RetHtml +=
                        "   </table></div>";
                }
                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }




        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideDeleteRoleRelationTemplate(
            string SiteId, 
            string RoleRelationTemplateId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteRoleRelationTemplate(null, SiteId, RoleRelationTemplateId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId).HtmlContent;
                OAjaxOut.InfoMessage = "Xóa thành công quan hệ vị trí";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }
    }
}
